package com.dxl.web.account.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dxl.bean.account.FunctionNode;
import com.dxl.common.annotation.RequirePermission;
import com.dxl.common.model.Page;
import com.dxl.common.model.RespData;
import com.dxl.common.model.easyui.TreeNode;
import com.dxl.common.utils.Utils;
import com.dxl.entity.account.Function;
import com.dxl.service.account.FunctionService;
import com.dxl.web.common.controller.BaseController;

/**
 * 功能点信息控制器
 * @author 邓小林
 */
@Controller
@RequestMapping("/account/function")
@RequirePermission(value={"FUNCTION-MODULE"}, name="功能点模块", desc="包括功能点增、删、改、查")
public class FunctionController extends BaseController {
	@Autowired
	private FunctionService functionService;
	
	@RequestMapping("")
	@RequirePermission(value={"FUNCTION-VIEW"}, name="功能点查看", desc="查看功能点信息")
	public ModelAndView function() {
		ModelAndView mv = new ModelAndView("account/function/list");
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/tree")
	@RequirePermission(value={"FUNCTION-VIEW"}, name="功能点查看", desc="查看功能点信息")
	public List<TreeNode> tree(String roleId) {
		return this.functionService.tree(roleId);
	}
	
	@ResponseBody
	@RequestMapping("/treegrid")
	@RequirePermission(value={"ROLE-VIEW"}, name="角色查看", desc="查看角色信息")
	public List<FunctionNode> treegrid() {
		return this.functionService.treeGrid();
	}
	
	@ResponseBody
	@RequestMapping("/list")
	@RequirePermission(value={"FUNCTION-VIEW"}, name="功能点查看", desc="查看功能点信息")
	public Page<Function> list(String parentId, String keyword, Integer state, Integer page, Integer rows) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		if (Utils.isNotNull(parentId)) {
			paramMap.put("parentId", parentId);
		}
		Page<Function> p = this.functionService.list(parentId, keyword, state, page, rows);
		return p;
	}
	
	@RequestMapping("/edit")
	@RequirePermission(value={"FUNCTION-VIEW"}, name="功能点查看", desc="查看功能点信息")
	public ModelAndView edit(String id) {
		ModelAndView mv = new ModelAndView("account/function/edit");
		if (Utils.isNotBlank(id)) {
			mv.addObject("entity", this.baseService.get(Function.class, id));
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/save")
	@RequirePermission(value={"FUNCTION-MODIFY"}, name="功能点修改", desc="修改功能点信息")
	public RespData save(Function function) {
		return functionService.save(function);
	}
	
	@ResponseBody
	@RequestMapping("/del")
	@RequirePermission(value={"FUNCTION-DEL"}, name="功能点删除", desc="删除功能点信息")
	public RespData del(String[] ids) {
		return functionService.del(ids);
	}

	@ResponseBody
	@RequestMapping("/state")
	@RequirePermission(value={"FUNCTION-STATE"}, name="功能点状态修改", desc="修改功能点状态")
	public RespData state(String[] ids, Integer state) {
		return functionService.state(ids, state);
	}
}
